package com.nfha.algorithm;

import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;

import com.nfha.algorithm.geo.crossline.CrosslineResult;
import com.nfha.algorithm.util.Logger;

public abstract class Exerciser implements Closeable{

	
	
	final private ExecutorService executor; 
	
	
    protected Logger logger=Logger.getConsoleLogger();


	public Exerciser() {
		super();
		executor=Executors.newCachedThreadPool();
	}
    
    
	public Exerciser(int nThreads) {
    	
    	executor=Executors.newFixedThreadPool(nThreads);
    }
	
	
	protected void execute(Runnable task) {
		// TODO Auto-generated method stub
		executor.execute(task);
	}

	
	@Override
	public void close() throws IOException {
		// TODO Auto-generated method stub
		executor.shutdown();
	}
	
	
	public <T> T readObject(File file) throws FileNotFoundException, IOException, ClassNotFoundException {
		T ret=null;
		try(
				FileInputStream fis=new FileInputStream(file);
				ObjectInputStream ois=new ObjectInputStream(fis);
				){
			
			ret=(T)ois.readObject();
		}
		return ret;
	}
	
    public void saveObject(Serializable data,File file) throws FileNotFoundException, IOException {
		
		try(
				FileOutputStream fos=new FileOutputStream(file);
				ObjectOutputStream oos=new ObjectOutputStream(fos);
				){
			oos.writeObject(data);
			
		}
		
	}
    
    
    public Logger getLogger() {
		return logger;
	}


	public void setLogger(Logger logger) {
		this.logger = logger;
	}
    
}
